/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import accessor.User;
import accessor.UserAccessor;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tools.EmailValidator;

/**
 *
 * @author QUYET
 */
@WebServlet(name = "add_user", urlPatterns = {"/add_user"})
public class add_user extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        UserAccessor accessor = new UserAccessor();
        EmailValidator validator = new EmailValidator();

        try {
            if (request.getParameter("register") == null) {
                request.getSession().removeAttribute("add_email");
                request.getSession().removeAttribute("add_first_name");
                request.getSession().removeAttribute("add_last_name");
                request.getSession().removeAttribute("val_email");
                request.getSession().removeAttribute("val_first_name");
                request.getSession().removeAttribute("val_last_name");
                request.getSession().removeAttribute("val_departmentID");
                request.getSession().removeAttribute("val_dateOfBirth");
                response.sendRedirect("admin/add_user.jsp");
            } else {
                String email = request.getParameter("email");
                String firstName = request.getParameter("first_name");
                String lastName = request.getParameter("last_name");
                int departmentID = Integer.parseInt(request.getParameter("departmentID"));
                if (!email.equals("")
                        && !firstName.equals("")
                        && !lastName.equals("")
                        && validator.validate(email)
                        && !accessor.isEmailExist(email)) {
                    User user = new User();
                    user.setEmail(email);
                    user.setPassword("123456");
                    user.setDepartmentID(departmentID);
                    int userID = accessor.insertUser(user);
                    if (userID > 0) {
                        user.setUserID(userID);
                        user.setFirstName(firstName);
                        user.setLastName(lastName);
                        user.setDateOfBirth(Date.valueOf(request.getParameter("dateOfBirth")));
                        System.out.println(user.getDateOfBirth().toString());
                        accessor.insertUserProfile(user);
                        request.getSession().removeAttribute("add_email");
                        request.getSession().removeAttribute("add_first_name");
                        request.getSession().removeAttribute("add_last_name");
                        request.getSession().setAttribute("mess_user", "<p>Add user successful!</p>");
                        response.sendRedirect("admin/list_user.jsp");
                    }
                } else {
                    if (accessor.isEmailExist(email)) {
                        request.getSession().setAttribute("add_email", "<p>The email already registered!</p>");
                    }
                    if (email.equals("")) {
                        request.getSession().setAttribute("add_email", "<p>You must enter your email address!</p>");
                    } else {
                        if (!validator.validate(email)) {
                            request.getSession().setAttribute("add_email", "<p>You must enter a valid email address!</p>");
                        }
                    }
                    if (firstName.equals("")) {
                        request.getSession().setAttribute("add_first_name", "<p>You must enter your first name!</p>");
                    }
                    if (lastName.equals("")) {
                        request.getSession().setAttribute("add_last_name", "<p>You must enter your last name!</p>");
                    }

                    request.getSession().setAttribute("val_email", email);
                    request.getSession().setAttribute("val_first_name", firstName);
                    request.getSession().setAttribute("val_last_name", lastName);
                    request.getSession().setAttribute("val_departmentID", request.getParameter("departmentID"));
                    request.getSession().setAttribute("val_dateOfBirth", request.getParameter("dateOfBirth"));
                    response.sendRedirect("admin/add_user.jsp");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
